"""
Case Type   : Compatibility
Case Name   : 验证逻辑操作符的bool值输出(&& || !)
Create At   : 2024/10/18
Owner       : liu-tong-8848
Description :
              1.修改参数值
              2.and操作符
              3.or操作符
              4.not操作符
              5.混合使用
              6.清理环境
Expect      :
              1.成功
              2.成功
              3.成功
              4.成功
              5.成功
              6.清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbBmode')
        self.db_user_node = Node(node='PrimaryDbBmode')

    def test_add_dimension(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        step1 = '--step1：修改参数值 expect：执行成功'
        self.log.info(step1)
        change_paramater_cmd = '''echo "dolphin.sql_mode = 'sql_mode_strict,sql_mode_full_group,ansi_quotes'" >> ''' \
                               + macro.DB_INSTANCE_PATH + '''/''' + macro.DB_PG_CONFIG_NAME
        change_paramater_result = self.db_user_node.sh(change_paramater_cmd).result()
        self.assertNotIn("ERROR", change_paramater_result, "执行失败" + step1)
        self.primary_sh.restart_db_cluster()

        step2 = ' --step2：and操作符 expect：执行成功'
        self.log.info(step2)
        sql = "select true && 0 as c;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("f", result, "执行失败" + step2)
        sql = "select false && false as c;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("f", result, "执行失败" + step2)
        sql = "select 1 && 1 as c;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("t", result, "执行失败" + step2)

        step3 = '--step3：or操作符 expect：执行成功'
        self.log.info(step3)
        sql = "select 1 || 0 as c;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("t", result, "执行失败" + step3)
        sql = "select true || 1 as c;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("t", result, "执行失败" + step3)
        sql = "select false || 0 as c;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("f", result, "执行失败" + step3)

        step4 = '--step4：not操作符 expect：执行成功'
        self.log.info(step4)
        sql = "select !1 as c;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("f", result, "执行失败" + step4)
        sql = "select !false as c; "
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("t", result, "执行失败" + step4)

        step5 = '--step5：混合使用 expect：执行成功'
        self.log.info(step5)
        sql = "select 1 &&(true || (!false)) || 0 as c;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("t", result, "执行失败" + step5)

    def tearDown(self):
        step6 = 'step6:清理环境 expect:清理环境成功'
        self.log.info(step6)
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "dolphin.b_compatibility_mode = on"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn("ERROR", gs_guc_set_result, "执行失败" + step6)
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "dolphin.sql_mode = 'sql_mode_strict,sql_mode_full_group,pipes_as_concat, ''' \
                         + ''' ansi_quotes,no_zero_date,pad_char_to_full_length,auto_recompile_function, ''' \
                         + ''' error_for_division_by_zero'"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn("ERROR", gs_guc_set_result, "执行失败" + step6)
        self.primary_sh.restart_db_cluster()
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
